package com.ruoyi.web.controller.material;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.material.domain.SmesMaterialOutInBill;
import com.ruoyi.mes.material.service.ISmesMaterialOutInBillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 原料出入库订单管理Controller
 *
 * @author simpsense
 * @date 2020-12-15
 */
@RestController
@RequestMapping("/material/materialOutInBill")
public class SmesMaterialOutInBillController extends BaseController
{
    @Autowired
    private ISmesMaterialOutInBillService smesMaterialOutInBillService;

    /**
     * 查询原料出入库订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('material:materialOutInBill:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmesMaterialOutInBill smesMaterialOutInBill)
    {
        startPage();
        List<SmesMaterialOutInBill> list = smesMaterialOutInBillService.selectSmesMaterialOutInBillList(smesMaterialOutInBill);
        return getDataTable(list);
    }

    /**
     * 导出原料出入库订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('material:materialOutInBill:export')")
    @Log(title = "原料出入库订单管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmesMaterialOutInBill smesMaterialOutInBill)
    {
        List<SmesMaterialOutInBill> list = smesMaterialOutInBillService.selectSmesMaterialOutInBillList(smesMaterialOutInBill);
        ExcelUtil<SmesMaterialOutInBill> util = new ExcelUtil<SmesMaterialOutInBill>(SmesMaterialOutInBill.class);
        return util.exportExcel(list, "materialOutInBill");
    }

    /**
     * 获取原料出入库订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:materialOutInBill:query')")
    @GetMapping(value = "/{outInBillNo}")
    public AjaxResult getInfo(@PathVariable("outInBillNo") String outInBillNo)
    {
        return AjaxResult.success(smesMaterialOutInBillService.selectSmesMaterialOutInBillById(outInBillNo));
    }

    /**
     * 新增原料出入库订单管理
     */
    @PreAuthorize("@ss.hasPermi('material:materialOutInBill:add')")
    @Log(title = "原料出入库订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmesMaterialOutInBill smesMaterialOutInBill)
    {
        switch (smesMaterialOutInBill.getBillType()){
            case "01"://生产领料
                return toAjax(smesMaterialOutInBillService.insertSmesMaterialOutBillNormal(smesMaterialOutInBill));
            case "02"://其他出库
                return toAjax(smesMaterialOutInBillService.insertSmesMaterialOutBillOther(smesMaterialOutInBill));
            case "03"://其他入库
                return toAjax(smesMaterialOutInBillService.insertSmesMaterialInBillOther(smesMaterialOutInBill));
//            default:
//                return toAjax(smesMaterialOutInBillService.insertSmesMaterialOutBillNormal(smesMaterialOutInBill));
        }
        return AjaxResult.error("请确认原料出入库单据类型！");
    }

    /**
     * 修改原料出入库订单管理
     */
    @PreAuthorize("@ss.hasPermi('material:materialOutInBill:edit')")
    @Log(title = "原料出入库订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmesMaterialOutInBill smesMaterialOutInBill)
    {
        return toAjax(smesMaterialOutInBillService.updateSmesMaterialOutInBill(smesMaterialOutInBill));
    }

    /**
     * 删除原料出入库订单管理
     */
    @PreAuthorize("@ss.hasPermi('material:materialOutInBill:remove')")
    @Log(title = "原料出入库订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{outInBillNos}")
    public AjaxResult remove(@PathVariable String[] outInBillNos)
    {
        return toAjax(smesMaterialOutInBillService.deleteSmesMaterialOutInBillByIds(outInBillNos));
    }
}